package org.example.springweibo.controller;

import org.example.springweibo.jsonresult.JsonResult;
import org.example.springweibo.pojo.dto.UserDTO;
import org.example.springweibo.pojo.entity.User;
import org.example.springweibo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 控制层  用于接收http请求
 *
 * @RestController  =  @Controller + @ResponseBody
 */

@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/reg")
    public JsonResult reg(UserDTO userDTO){
        userService.regUser(userResult(userDTO));
        return JsonResult.ok();
    }

    @PostMapping("/login")
    public JsonResult login(UserDTO user){
        User data = userService.loginUser(userResult(user));
        return JsonResult.ok(data);
    }

    private User userResult(UserDTO userDTO){
        User user = new User();
        user.setId(userDTO.getId());
        user.setUsername(userDTO.getUsername());
        user.setNickname(userDTO.getNickname());
        user.setPassword(userDTO.getPassword());
        return user;
    }
}
